﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ignitron.EEngine.Maths;
using GameEngine;
using Board3D.Models;

namespace Board3D.Animation
{
    /// <summary>
    /// Class for building animation of a move
    /// </summary>
    internal static class MoveAnimationBuilder
    {
        private static BoardModel _boardModel;

        /// <summary>
        /// Returns figure position
        /// </summary>
        private static EVector3 GetFigurePosition(int row, int col)
        {
            float X = 0;
            float Y = 0;
            float Z = 0;

            _boardModel.GetFigurePosition(row, col, out X, out Y, out Z);

            return new EVector3(X, Y, Z);
        }

        /// <summary>
        /// The main buiding method
        /// </summary>
        public static AnimationBlock Build(Move completeMove, BoardModel boardModel)
        {
            _boardModel = boardModel;

            AnimationBlock animBlock = new AnimationBlock();
            Move move = completeMove;
            Position figurePos = move.startPos;
            while (move != null)
            {
                int animLength = 500 * Math.Abs(move.endPos.Row - move.startPos.Row);

                if (move.RemovingFigure == true)
                {
                    animBlock.AddAnimation(new JumpMoveAnimation(figurePos,
                        GetFigurePosition(move.startPos.Row, move.startPos.Col),
                        GetFigurePosition(move.endPos.Row, move.endPos.Col),
                        TimeSpan.FromMilliseconds(animLength)));

                    animBlock.AddAnimation(new JumpedFigureAnimation(move.removedFigurePlace,
                            TimeSpan.FromMilliseconds(500)));
                }
                else
                {
                    animBlock.AddAnimation(new SlideMoveAnimation(figurePos,
                        GetFigurePosition(move.startPos.Row, move.startPos.Col),
                        GetFigurePosition(move.endPos.Row, move.endPos.Col),
                        TimeSpan.FromMilliseconds(animLength)));
                }


                move = move.nextMove;
            }

            return animBlock;
        }
    }
}
